﻿using System.Windows.Controls;
using SomethingSpacial.Views.Default;

namespace SomethingSpacial.Views
{
    /// <summary>
    /// The login form view.
    /// </summary>
    public partial class LoginForm : UserControl
    {
        public LoginForm()
        {
            InitializeComponent();

            Loaded += new System.Windows.RoutedEventHandler(LoginForm_Loaded);
        }

        void LoginForm_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!(App.CurrentUser.IsLoggedIn))
            {
                LoadToLogin.Begin();
            }
            else
            {
                App.CurrentUser.IsLoggedIn = false;
                ((SomethingSpacial.Views.Default.DefaultView)(App.Current.RootVisual)).SetLoginState();
                ((SomethingSpacial.Views.Default.DefaultView)(App.Current.RootVisual)).ContentTarget.Children.Clear();
            }
        }

        private void Login_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!(string.IsNullOrEmpty(UserName.Text)) && !(string.IsNullOrEmpty(UserPassword.Password)))
            {
                SomethingSpacial.Model.User ThisUser = new SomethingSpacial.Model.User();
                App.CurrentUser.IsLoggedIn = ThisUser.LoginUser(UserName.Text, UserPassword.Password);
                ((SomethingSpacial.Views.Default.DefaultView)(App.Current.RootVisual)).SetLoginState();
            }
            ((SomethingSpacial.Views.Default.DefaultView)(App.Current.RootVisual)).ContentTarget.Children.Clear();
        }

        public bool FirstTime = true;

        private void UserName_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            if (FirstTime)
            {
                FirstTime = false;
                UserName.Text = string.Empty;
            }
        }
    }
}
